package Java8新特性;

import org.junit.Before;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class StreamDemo {

    List<Person> personList = new ArrayList<>();

    @Before
    public void before() {
        personList.add(new Person("Tom", 8900, 23, "male", "New York"));
        personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
        personList.add(new Person("Lily", 7800, 21, "female", "Washington"));
        personList.add(new Person("Anni", 8200, 24, "female", "New York"));
        personList.add(new Person("Owen", 9500, 25, "male", "New York"));
        personList.add(new Person("Alisa", 7900, 26, "female", "New York"));
    }

    @Test
    public void createStream1() {
        List<String> list = Arrays.asList("a", "b", "c");
        //创建一个顺序流
        Stream<String> stream = list.stream();
        //创建一个并行流
        Stream<String> parallelStream = list.parallelStream();
        //通过 parallel() 把顺序流转换成并行流
        Optional<String> findFirst = list.stream().parallel().filter(x -> x.equals("a")).findFirst();
    }

    @Test
    public void createStream2() {
        int[] array = {1, 3, 5, 6, 8};
        IntStream stream = Arrays.stream(array);
    }

    @Test
    public void createStream3() {
        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6);

        Stream<Integer> stream2 = Stream.iterate(0, x -> x + 3).limit(4);
        stream2.forEach(System.out::println);

        Stream<Double> stream3 = Stream.generate(Math::random).limit(3);
        stream3.forEach(System.out::println);
    }

    @Test
    public void testMethod1() {
        List<Integer> list = Arrays.asList(7, 6, 9, 3, 8, 2, 1);

        //遍历出符合条件的元素
        list.stream().filter(x -> x > 6).forEach(System.out::println);
        //匹配第一个
        Optional<Integer> findFirst = list.stream().filter(x -> x > 6).findFirst();
        //匹配任意(适用于并行流)
        Optional<Integer> findAny = list.parallelStream().filter(x -> x > 6).findAny();
        //是否包含符合特定条件的元素
        boolean anyMatch = list.stream().anyMatch(x -> x < 6);

        System.out.println("匹配第一个值: " + findFirst.get());
        System.out.println("匹配任意一个值: " + findAny.get());
        System.out.println("是否存在大于6的值: " + anyMatch);
    }

    @Test
    public void testMethod2() {
        List<Integer> list = Arrays.asList(6, 7, 3, 8, 1, 2, 9);
        list.stream().filter(x -> x > 7).forEach(System.out::println);

        List<String> filterList = this.personList.stream().filter(x -> x.getSalary() > 8000).map(Person::getName).collect(Collectors.toList());
        System.out.println("工资高于 8000 的员工姓名: " + filterList);
    }

    @Test
    public void testMethod3() {
        List<String> list = Arrays.asList("adnm", "admmt", "pot", "xbangd", "weoujgsd");
        Optional<String> max = list.stream().max(Comparator.comparing(String::length));
        System.out.println("最长的字符串: " + max.get());
    }

    @Test
    public void testMethod4() {
        List<Integer> list = Arrays.asList(7, 4, 9, 4, 11, 6);
        //自然排序
        Optional<Integer> max1 = list.stream().max(Integer::compareTo);
        //自定义排序
        Optional<Integer> max2 = list.stream().max(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });

        System.out.println("自然排序的最大值: " + max1.get());
        System.out.println("自定义排序的最大值: " + max2.get());
    }

}
